package com.ms.qw.application.checkpoint;

import com.ms.qw.enums.RecetaEstado;
import com.ms.qw.application.mappers.CheckPointMapper;
import com.ms.qw.client.dto.CheckPointDTO;
import com.ms.qw.domain.model.CheckPoint;
import com.ms.qw.domain.model.CheckPointRepository;
import com.ms.qw.client.dto.CheckPointFlt;
import com.ms.qw.server.infrastructure.persistence.exception.ApplicationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.util.*;

@Component
public class RegistroCheckPointAppImpl implements RegistroCheckPointApp {


    private static final Logger LOG = LoggerFactory.getLogger(RegistroCheckPointAppImpl.class);
/*
    @Autowired
    DTOMapperService mapperService;
*/



    @Autowired
    CheckPointRepository checkPointRepository;

    @Autowired
    CheckPointMapper checkPointMapper;

    @Autowired
    JavaMailSender mailSender;


    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void save(CheckPoint checkPoint, boolean updateMode) {
        checkPointRepository.persist(checkPoint);
    }


    private JdbcTemplate jdbcTemplate;

    @Autowired
    DataSource dataSource;

    @PostConstruct
    public void init() {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void delete(CheckPoint checkPoint) {
        checkPointRepository.remove(checkPoint);
        checkPointRepository.flush();
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void updateCheckPoints(Long usuarioId) {
        CheckPointFlt checkPointFlt=new CheckPointFlt();
        checkPointFlt.setUsuarioId(usuarioId);
        List<CheckPointDTO> lista = checkPointMapper.findLast(checkPointFlt);
        for (CheckPointDTO item : lista) {
            finalizar(item.getCheckPointId());
        }
    }



    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void finalizar(Long checkPointId) {
        Map map = checkPointMapper.findResumen(checkPointId);

        if (map != null) {
            if(map.get("ANNO")==null){
                throw new ApplicationException("El archivo tiene formato incorrecto");
            }
            int maxYear = (Integer) map.get("ANNO");
            CheckPoint checkPoint = checkPointRepository.find(checkPointId);
            checkPoint.setEstado(RecetaEstado.TERMINADO);
            checkPoint.setFechaContable((Date) map.get("FECHA_CONTABLE"));
            checkPoint.setAnno1(String.valueOf(maxYear - 3));
            checkPoint.setAnno2(String.valueOf(maxYear - 2));
            checkPoint.setAnno3(String.valueOf(maxYear - 1));
            checkPoint.setAnno4(String.valueOf(maxYear));
            checkPoint.setTotalAgrupacion((long) (Integer) map.get("TOTAL_AGRUPACION"));
            checkPoint.setTotalCuentaContable((long) (Integer) map.get("TOTAL_CUENTA_CONTABLE"));
            checkPointRepository.merge(checkPoint);
        }
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void finalizarResumen(Long checkPointId) {
        CheckPoint checkPoint = checkPointRepository.find(checkPointId);
        checkPoint.setEstado(RecetaEstado.TERMINADO);
        checkPointRepository.merge(checkPoint);
    }



}
